package com.susallwave.srcb.utils;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.Map;

public class IpUtil {
    /**
     * 获取用户真实IP地址，不使用request.getRemoteAddr();的原因是有可能用户使用了代理软件方式避免真实IP地址,
     *
     * 可是，如果通过了多级反向代理的话，X-Forwarded-For的值并不止一个，而是一串IP值，究竟哪个才是真正的用户端的真实IP呢？
     * 答案是取X-Forwarded-For中第一个非unknown的有效IP字符串。
     *
     * 如：X-Forwarded-For：192.168.1.110, 192.168.1.120, 192.168.1.130,
     * 192.168.1.100
     *
     * 用户真实IP为： 192.168.1.110
     *
     * @param request
     * @return
     */
    public static String getIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
            if("127.0.0.1".equals(ip)||"0:0:0:0:0:0:0:1".equals(ip)){
                //根据网卡取本机配置的IP
                InetAddress inet=null;
                try {
                    inet = InetAddress.getLocalHost();
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
                ip= inet.getHostAddress();
            }
        }
        return ip;
    }

    public static Map<String,String> getPcInfo() throws IOException {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        System.out.println("浏览器基本信息："+request.getHeader("user-agent"));
        System.out.println("客户端系统名称："+System.getProperty("os.name"));
        System.out.println("客户端系统版本："+System.getProperty("os.version"));
        System.out.println("客户端操作系统位数："+System.getProperty("os.arch"));
        System.out.println("HTTP协议版本："+request.getProtocol());
        System.out.println("请求编码格式："+request.getCharacterEncoding());
        System.out.println("Accept："+request.getHeader("Accept"));
        System.out.println("Accept-语言："+request.getHeader("Accept-Language"));
        System.out.println("Accept-编码："+request.getHeader("Accept-Encoding"));
        System.out.println("Connection："+request.getHeader("Connection"));
        System.out.println("Cookie："+request.getHeader("Cookie"));
        System.out.println("客户端发出请求时的完整URL"+request.getRequestURL());
        System.out.println("请求行中的资源名部分"+request.getRequestURI());
        System.out.println("请求行中的参数部分"+request.getRemoteAddr());
        System.out.println("客户机所使用的网络端口号"+request.getRemotePort());
        System.out.println("WEB服务器的IP地址"+request.getLocalAddr());
        System.out.println("WEB服务器的主机名"+request.getLocalName());
        System.out.println("客户机请求方式"+request.getMethod());
        System.out.println("请求的文件的路径"+request.getServerName());
        System.out.println("请求体的数据流"+request.getReader());
        BufferedReader br=request.getReader();
        String res = "";
        while ((res = br.readLine()) != null) {
            System.out.println("request body:" + res);
        }
        System.out.println("请求所使用的协议名称"+request.getProtocol());
        System.out.println("请求中所有参数的名字"+request.getParameterNames());
        Enumeration enumNames= request.getParameterNames();
        while (enumNames.hasMoreElements()) {
            String key = (String) enumNames.nextElement();
            System.out.println("参数名称："+key);
        }
        return null;
    }
}
